<script setup>
import { usePlayerStore, useToolStore } from '@/stores'
import { computed } from 'vue'
import { onMounted, onUnmounted, ref, watch } from 'vue'
import { controlInfoTree, toTree } from '@/airCityApi/layerTree'
const playerStore = usePlayerStore()
const toolsStore = useToolStore()
const filterText = ref('')
const curClicked = ref()
const styleValue = ref(0)
const showloding = ref(false)
const styleList = ref([
  {
    value: 0,
    label: '默认'
  },
  {
    value: 1,
    label: 'X光'
  },
  {
    value: 2,
    label: '纯色'
  },
  {
    value: 3,
    label: '水晶体'
  }
])
const defaultProps = ref({
  children: 'children',
  label: 'label'
})
const color = ref('rgb(255, 255, 255)')
const infoTree = computed(() => toolsStore.InfoTree)
const defaultInfoTree = computed(() => playerStore.TreeInfo)

const defaultCheck = computed(() => {
  return (
    defaultInfoTree.value &&
    defaultInfoTree.value.map((item) => {
      if (item.visiblity && item.type !== 'EPT_Folder' && item.type !== 'EPT_World') {
        return item.iD
      }
    })
  )
})
const defaultExpanded = computed(() => {
  let arr = ['ProjectTree_Root']
  return defaultInfoTree.value
    .map((item) => {
      if (arr.indexOf(item.iD) > -1) {
        return item.iD
      }
    })
    .filter((i) => i)
})
const shuaxin = async () => {
  showloding.value = true
  const res = await __g.infoTree.get()
  playerStore.TreeInfo = res.infotree
  toolsStore.InfoTree = toTree(res.infotree)
  showloding.value = false
}
const zoom = ref(true)
const zoominout = () => {
  if (zoom.value) {
    zoom.value = false
  } else {
    zoom.value = true
  }
}
const clearstyle = () => {
  curClicked.value = null
}
// watch(defaultInfoTree, () => {}, { deep: true });

// 方法
const closeBox = () => {
  toolsStore.LayerTreeShow = false
}

const filterNode = (value, data) => {
  if (!value) return true
  return data.label.indexOf(value) !== -1
}
const Tree = ref()
// 过滤
watch(filterText, () => {
  Tree.value.filter(filterText.value)
})
const check = async (a, b, c, d) => {
  const AllIds = defaultInfoTree.value
    .map((item) => item.type !== 'EPT_Folder' && item.type !== 'EPT_World' && item.iD)
    .filter((i) => i)
  const NeedShow = []
  const NeedHide = []
  AllIds.forEach((item) => {
    if (b.checkedKeys.includes(item)) {
      NeedShow.push(item)
    } else {
      NeedHide.push(item)
    }
  })
  __g.infoTree.show(NeedShow)
  __g.infoTree.hide(NeedHide)
  filterText.value = ''
}
const goCenter = (data) => {
  if (!data.children) {
    __g.tileLayer.focus(data.id)
  }
}

const handleNodeClick = (data) => {
  if (!data.children) {
    let cur = defaultInfoTree.value.find((item) => {
      return item.iD === data.id
    })
    curClicked.value = data
    color.value = cur.color ? cur.color.replace('RGB', 'rgb') : 'rgb(255,255,255)'
    styleValue.value = cur.style
  } else {
    curClicked.value = null
  }
}
const styleChange = (value) => {
  controlInfoTree(curClicked.value.id, 'style', { style: value })
}
const colorChange = (value) => {
  controlInfoTree(curClicked.value.id, 'style', {
    color: value.replace('rgb', 'RGB')
  })
}
onMounted(async () => {
  // shuaxin();
})
onUnmounted(() => {
  shuaxin()
})
</script>
<template>
  <div @click.stop="clearstyle" id="layertree" v-drag>
    <div class="heard">
      <div class="heard-title">图层信息</div>
      <div @click="shuaxin" class="shuaixn">
        <el-icon><Refresh /></el-icon>
      </div>
      <div @click="closeBox" class="close">
        <el-icon><Close /></el-icon>
      </div>
      <div @click="zoominout" class="zoominout">
        <el-icon v-if="zoom"><ArrowUp /></el-icon>
        <el-icon v-else><ArrowDown /></el-icon>
      </div>
    </div>
    <div v-if="zoom" class="search">
      <input placeholder="输入关键字进行过滤" v-model="filterText" type="text" class="inputbox" />
      <el-icon><Search /></el-icon>
    </div>

    <el-tree
      v-if="zoom"
      class="tree"
      :data="infoTree"
      show-checkbox
      node-key="id"
      ref="Tree"
      :props="defaultProps"
      :check-strictly="false"
      :filter-node-method="filterNode"
      @node-click="handleNodeClick"
      @check="check"
      :default-checked-keys="defaultCheck"
      :default-expanded-keys="defaultExpanded"
    >
      <template #default="node">
        <span class="node-label customTreeNode">
          <span :title="node.data.label" class="text" @dblclick="goCenter(node.data)">
            {{ node.data.label }}
          </span>
        </span>
      </template>
    </el-tree>
    <div @click.stop v-if="zoom" class="set-style">
      <el-color-picker
        v-if="curClicked"
        v-model="color"
        color-format="rgb"
        @change="colorChange"
      ></el-color-picker>
      <el-select v-if="curClicked" v-model="styleValue" @change="styleChange" placeholder="请选择">
        <el-option
          v-for="item in styleList"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
    </div>
  </div>
</template>

<style lang="scss" scoped>
#layertree {
  width: 322px;
  border-radius: 8px;
  font-size: 14px;
  background: linear-gradient(360deg, rgba(14, 26, 42, 0.4) 0%, rgba(14, 26, 42, 0.8) 100%);
  z-index: 10;
  border: 1px solid rgba(122, 250, 254, 0.562);
  position: absolute;
  top: 174px;
  left: 30px;
  transform: translateX(0) !important;
  .heard {
    position: relative;
    width: 100%;
    height: 40px;
    border-bottom: 1px solid rgba(122, 250, 254, 0.603);
    .heard-title {
      position: absolute;
      top: 10px;
      left: 12px;
      font-size: 14px;

      font-family: MicrosoftYaHei;
      color: #7afafe;
    }
    .close {
      position: absolute;
      top: 10px;
      right: 12px;
      color: #fff;
      font-size: 16px;

      cursor: pointer;
      &:hover {
        color: #7afafe;
      }
    }
    .zoominout {
      position: absolute;
      top: 10px;
      right: 35px;
      color: #fff;
      font-size: 16px;

      cursor: pointer;
      &:hover {
        color: #7afafe;
      }
    }
    .shuaixn {
      position: absolute;
      top: 12px;
      left: 82px;
      color: #fff;
      font-size: 16px;

      cursor: pointer;
      &:hover {
        color: #7afafe;
      }
    }
  }
  .search {
    position: relative;
    top: 10px;

    left: 0px;
    right: 0px;
    width: 300px;
    margin: auto;
    background: rgba(14, 26, 42, 0.2);
    border-radius: 4px;
    height: 32px;

    border: 1px solid rgba(255, 255, 255, 0.5);
    .inputbox {
      width: 260px;
      position: absolute;
      top: 0px;
      left: 0px;
      background-color: transparent;
      height: 32px;
      border: none;
      padding: 0 10px;
      outline: 0 none;
      color: #fff;
    }
    i {
      position: absolute;
      top: 8px;
      right: 12px;
      color: #88929d;
    }
  }
  .set-style {
    width: 100%;
    position: relative;
    height: 35px;
    bottom: 10px;
    padding: 0 10px;
    display: flex;
    justify-content: space-around;
    align-items: center;
    .el-select {
      width: 240px;
      margin-left: 5px;
    }
    .el-color-picker {
      width: 34px;
      height: 34px;
    }
  }

  .el-tree {
    font-weight: 400;
    color: #ffffff;
  }

  .tree {
    width: 300px;
    height: 500px;
    margin: auto;
    margin-bottom: 60px;
    overflow: auto;
    background-color: transparent;
    position: relative;
    top: 20px;
    left: 0px;
    right: 0px;
  }
}

:deep() {
  .el-tree {
    background: none;
    color: rgba(255, 255, 255, 1);
  }

  .el-tree-node__content,
  .el-tree-node__content:hover {
    background-color: inherit !important;
  }

  // .el-tree-node__expand-icon {
  //   color: #fff;
  //   display: none !important;
  // }

  /* 选中节点时的颜色 */
  .is-current .el-tree-node__content {
    color: #fff; /* 蓝色字体 */
    font-weight: 700;
  }

  .el-tree-node__content.is-current {
    background: linear-gradient(
      270deg,
      rgba(250, 49, 62, 0) 0%,
      rgba(122, 250, 254, 0.562) 52%,
      rgba(247, 3, 3, 0) 100%
    ) !important; /* 浅蓝色背景 */
  }

  .el-tree-node__content:hover {
    background-color: rgba(255, 255, 255, 0);
    background: linear-gradient(
      270deg,
      rgba(250, 49, 62, 0) 0%,
      rgba(122, 250, 254, 0.562) 52%,
      rgba(247, 3, 3, 0) 100%
    ); /* 浅蓝色背景 */
  }
}
</style>
